from .embeddings import get_embedding
from .build_index import load_index_db, load_doc_content, load_doc_name, load_doc_api
from .clean_text import clean_text
import logging
import os

# 配置日志
logger = logging.getLogger(__name__)

def search_doc(query: str) -> (str, str):
    logger.debug(f"search_doc/query:{query}")

    index = load_index_db()
    logger.info(f"索引总数:{index.ntotal}")

    query_embedding = get_embedding(clean_text(query))
    logger.debug(f"query_embedding:{query_embedding}")

    distances, indices = index.search(query_embedding.reshape(1, -1), k=2)

    logger.debug(f"indices:{indices}")

    doc_index = indices[0][0]
    doc_content = load_doc_content(doc_index)
    doc_name = load_doc_name(doc_index)
    api_content = load_doc_api(doc_name)

    return doc_content, api_content

